package org.jeecg.modules.wlhy.hyproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyproject.entity.HyProject;
import org.jeecg.modules.wlhy.hyproject.mapper.HyProjectMapper;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: hy_project
 * @Author: jeecg-boot
 * @Date: 2022-10-11
 * @Version: V1.0
 */
@Service
public class HyProjectServiceImpl extends ServiceImpl<HyProjectMapper, HyProject> implements IHyProjectService {


    @Autowired
    ISysUserService sysUserService;

    @Override
    @CacheEvict(value = "dataPermission:*",allEntries=true)
    public boolean updateById(HyProject project) {
        return baseMapper.updateById(project) == 1;
    }

    @Override
    @CacheEvict(value = "dataPermission:*",allEntries=true)
    public boolean removeById(String id) {
        return baseMapper.deleteById(id) == 1;
    }

    @Override
    @CacheEvict(value = "dataPermission:*",allEntries=true)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return baseMapper.deleteBatchIds(idList) == 1;
    }


    @Override
    @Cacheable(unless = "#result == null", cacheNames = "dataPermission:", key = "#userId")
    public String getListById(String userId) {

        SysUser sysUser = sysUserService.getById(userId);
        if (!"admin".equals(sysUser.getUsername())) {
            //获取用户数据权限
            List<HyProject> list = this.list(new QueryWrapper<HyProject>().like("project_users", userId));
            StringBuilder ids = new StringBuilder();
            for (HyProject hy : list) {
                String[] split = hy.getProjectEnterprises().split(",");
                for (int j = 0; j < split.length; j++) {
                    ids.append(",");
                    ids.append(split[j]);
                }
            }
            return ids.toString();
        } else {
            return "";
        }
    }


}
